const utils = (() => {
  return {
    compare: (keys = [], sort = 1) => {
      return (obj1, obj2) => {
        let item1 = obj1;
        let item2 = obj2;
        keys.forEach(key => {
          item1 = item1[key];
          item2 = item2[key];
        });
        if (!utils.validateIsNumber(item1)) {
          return 1;
        }
        if (!utils.validateIsNumber(item2)) {
          return -1;
        }
        item1 = item1 - 0;
        item2 = item2 - 0;
        if (item1 < item2) {
          return sort === 1 ? -1 : 1;
        } else if (item1 > item2) {
          return sort === 1 ? 1 : -1;
        } else {
          return 0;
        }
      };
    },
    validateIsNumber: data => {
      return !(
        data === null ||
        data === undefined ||
        data === "" ||
        isNaN(data - 0)
      );
    },
    validateIsNotEmpty: data => {
      return !(
        data === null ||
        data === undefined ||
        Object.keys(data).length === 0
      );
    },
    validateAqi: data => {
      let className = "";
      if (!utils.validateIsNumber(data)) {
        className = "";
      } else if (data - 0 <= 50) {
        className = "warn-color-1";
      } else if (data <= 100) {
        className = "warn-color-2";
      } else if (data - 0 <= 150) {
        className = "warn-color-3";
      } else if (data - 0 <= 200) {
        className = "warn-color-4";
      } else if (data - 0 <= 300) {
        className = "warn-color-5";
      } else {
        className = "warn-color-6";
      }
      return className;
    },
    validateAir: data => {
      let content = "";
      if (!utils.validateIsNumber(data)) {
        content = "未知";
      } else if (data - 0 <= 50) {
        content = "优";
      } else if (data <= 100) {
        content = "良";
      } else if (data - 0 <= 150) {
        content = "轻度污染";
      } else if (data - 0 <= 200) {
        content = "中度污染";
      } else if (data - 0 <= 300) {
        content = "重度污染";
      } else {
        content = "严重污染";
      }
      return content;
    },
    validateSo2: data => {
      let className = "";
      if (!utils.validateIsNumber(data)) {
        className = "";
      } else if (data - 0 <= 150) {
        className = "warn-color-1";
      } else if (data - 0 <= 500) {
        className = "warn-color-2";
      } else if (data - 0 <= 650) {
        className = "warn-color-3";
      } else if (data - 0 <= 800) {
        className = "warn-color-4";
      } else {
        className = "warn-color-5";
      }
      return className;
    },
    validateNo2: data => {
      let className = "";
      if (!utils.validateIsNumber(data)) {
        className = "";
      } else if (data - 0 <= 100) {
        className = "warn-color-1";
      } else if (data - 0 <= 200) {
        className = "warn-color-2";
      } else if (data - 0 <= 700) {
        className = "warn-color-3";
      } else if (data - 0 <= 1200) {
        className = "warn-color-4";
      } else if (data - 0 <= 2340) {
        className = "warn-color-5";
      } else {
        className = "warn-color-6";
      }
      return className;
    },
    validatePm10: data => {
      let className = "";
      if (!utils.validateIsNumber(data)) {
        className = "";
      } else if (data - 0 <= 50) {
        className = "warn-color-1";
      } else if (data - 0 <= 150) {
        className = "warn-color-2";
      } else if (data - 0 <= 150) {
        className = "warn-color-3";
      } else if (data - 0 <= 350) {
        className = "warn-color-4";
      } else if (data - 0 <= 420) {
        className = "warn-color-5";
      } else {
        className = "warn-color-6";
      }
      return className;
    },
    validateCo: data => {
      let className = "";
      if (!utils.validateIsNumber(data)) {
        className = "";
      } else if (data - 0 <= 5) {
        className = "warn-color-1";
      } else if (data - 0 <= 10) {
        className = "warn-color-2";
      } else if (data - 0 <= 35) {
        className = "warn-color-3";
      } else if (data - 0 <= 60) {
        className = "warn-color-4";
      } else if (data - 0 <= 90) {
        className = "warn-color-5";
      } else {
        className = "warn-color-6";
      }
      return className;
    },
    validateO3: data => {
      let className = "";
      if (!utils.validateIsNumber(data)) {
        className = "";
      } else if (data - 0 <= 160) {
        className = "warn-color-1";
      } else if (data - 0 <= 200) {
        className = "warn-color-2";
      } else if (data - 0 <= 300) {
        className = "warn-color-3";
      } else if (data - 0 <= 400) {
        className = "warn-color-4";
      } else if (data - 0 <= 800) {
        className = "warn-color-5";
      } else {
        className = "warn-color-6";
      }
      return className;
    },
    validatePm2_5: data => {
      let className = "";
      if (!utils.validateIsNumber(data)) {
        className = "";
      } else if (data - 0 <= 35) {
        className = "warn-color-1";
      } else if (data - 0 <= 75) {
        className = "warn-color-2";
      } else if (data - 0 <= 115) {
        className = "warn-color-3";
      } else if (data - 0 <= 150) {
        className = "warn-color-4";
      } else if (data - 0 <= 250) {
        className = "warn-color-5";
      } else {
        className = "warn-color-6";
      }
      return className;
    },
    validateWindDirection(wd, ws) {
      let result = "";
      if (!utils.validateIsNumber(wd) || !utils.validateIsNumber(ws)) {
        result = "无风";
      } else if (ws - 0 <= 0.2) {
        result = "无风";
      } else if (wd - 0 > 337.5 || wd - 0 < 22.5) {
        result = "北风";
      } else if (wd - 0 >= 22.5 && wd - 0 <= 67.5) {
        result = "东北风";
      } else if (wd - 0 > 67.5 && wd - 0 < 112.5) {
        result = "东风";
      } else if (wd - 0 >= 112.5 && wd - 0 <= 157.5) {
        result = "东南风";
      } else if (wd - 0 > 157.5 && wd - 0 < 202.5) {
        result = "南风";
      } else if (wd - 0 >= 202.5 && wd - 0 <= 247.5) {
        result = "西南风";
      } else if (wd - 0 > 247.5 && wd - 0 < 292.5) {
        result = "西风";
      } else if (wd - 0 >= 292.5 && wd - 0 <= 337.5) {
        result = "西北风";
      }
      return result;
    },
    validateWindSpeed(data) {
      let result = "";
      if (!utils.validateIsNumber(data)) {
        result = "";
      } else if (data - 0 <= 0.2) {
        result = "无风";
      } else if (data - 0 <= 1.5) {
        result = "1级";
      } else if (data - 0 <= 3.3) {
        result = "2级";
      } else if (data - 0 <= 5.4) {
        result = "3级";
      } else if (data - 0 <= 7.9) {
        result = "4级";
      } else if (data - 0 <= 10.7) {
        result = "5级";
      } else if (data - 0 <= 13.8) {
        result = "6级";
      } else if (data - 0 <= 17.1) {
        result = "7级";
      } else if (data - 0 <= 20.7) {
        result = "8级";
      } else if (data - 0 <= 24.4) {
        result = "9级";
      } else if (data - 0 <= 28.4) {
        result = "10级";
      } else if (data - 0 <= 32.6) {
        result = "11级";
      } else if (data - 0 <= 36.9) {
        result = "12级";
      } else if (data - 0 <= 41.4) {
        result = "13级";
      } else if (data - 0 <= 46.1) {
        result = "14级";
      } else if (data - 0 <= 50.9) {
        result = "15级";
      } else if (data - 0 <= 56.0) {
        result = "16级";
      } else if (data - 0 <= 61.2) {
        result = "17级";
      } else {
        result = "17级以上";
      }
      return result;
    }
  };
})();
export default utils;
